package com.mls.guardian.executor.slave;

import com.mls.guardian.container.SlaveContainer;
import com.mls.guardian.dataobj.BackupServerObj;
import com.mls.guardian.protocol.RemoteBody;
import com.mls.guardian.protocol.RemoteCommand;
import com.mls.guardian.protocol.body.BackupInfoBody;
import com.mls.guardian.remoting.executor.RemoteExecutor;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengrenfang on 15/11/11.
 */
public class SlaveGetBackupExecutor implements RemoteExecutor {
    private static final Logger log = LoggerFactory.getLogger(SlaveGetBackupExecutor.class);
    private SlaveContainer container;

    public SlaveGetBackupExecutor(SlaveContainer container) {
        this.container = container;
    }

    @Override
    public RemoteBody execute(ChannelHandlerContext ctx, RemoteCommand request) throws Exception {
        BackupInfoBody response = new BackupInfoBody();

        log.info("slave 收到GetBackup请求 请求id是{}", request.getRequestId());
        BackupInfoBody requestBody = (BackupInfoBody) request.getRemoteBody();
        response.setProjectName(requestBody.getProjectName());
        response.setVersionTag(requestBody.getVersionTag());

        BackupServerObj backupServerObj = new BackupServerObj();
        backupServerObj.setList(container.getAppController().getBackup(requestBody.getProjectName(), requestBody.getVersionTag()));

        List<BackupServerObj> backupServerObjList = new ArrayList<BackupServerObj>();
        backupServerObjList.add(backupServerObj);

        response.setBackupServerObjList(backupServerObjList);

        return response;
    }
}